<html>
<head>
    <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>第九章 Groovy快速入门 Chapter&nbsp;9.&nbsp;Groovy Quickstart</title>
    <link xmlns:xslthl="http://xslthl.sf.net" type="text/css" rel="stylesheet" href="../css/base.css">
    <link xmlns:xslthl="http://xslthl.sf.net" type="text/css" rel="stylesheet" href="../css/docs.css">
    <link xmlns:xslthl="http://xslthl.sf.net" type="text/css" rel="stylesheet" href="../css/userguide.css">
    <meta content="DocBook XSL Stylesheets V1.75.2" name="generator">
    <link rel="home" href="../userguide.html" title="Gradle User Guide">
    <link rel="up" href="../userguide.html" title="Gradle User Guide">
    <link rel="prev" href="../ch08/artifact_dependencies_tutorial.html" title="Chapter&nbsp;8.&nbsp;Dependency Management Basics">
    <link rel="next" href="../ch10/web_project_tutorial.html" title="Chapter&nbsp;10.&nbsp;Web Application Quickstart"></head>
<body>
    <div class="navheader">
        <div>
            <div class="navbar">
                <a xmlns:xslthl="http://xslthl.sf.net" href="../ch08/artifact_dependencies_tutorial.html" title="Chapter&nbsp;8.&nbsp;Dependency Management Basics">上一章</a>
                <span>|</span>
                <a xmlns:xslthl="http://xslthl.sf.net" href="../userguide.html" title="Gradle User Guide">目录</a>
                <span>|</span>
                <a xmlns:xslthl="http://xslthl.sf.net" href="../ch10/web_project_tutorial.html" title="Chapter&nbsp;10.&nbsp;Web Application Quickstart">下一章</a>
            </div>
        </div>
    </div>
    <div class="chapter">
        <div class="titlepage">
            <div>
                <div>
                    <h1 xmlns:xslthl="http://xslthl.sf.net">
                        <a name="tutorial_groovy_projects"></a>
                        第九章. Groovy快速入门
                    </h1>
                    <h3>Chapter&nbsp;9.&nbsp;Groovy Quickstart</h3>
                </div>
            </div>
        </div>
        <p>
            要构建一个Groovy项目，你需要使用<em class="firstterm">Groovy插件</em>。该插件扩展了Java插件，对你的项目增加了Groovy的编译功能. 你的项目可以包含Groovy源码，Java源码，或者两者都包含。在其他各方面，Groovy项目与我们在 <a class="xref" href="../ch07/tutorial_java_projects.html">第七章 <i>Java快速入门</i></a> 中所看到的Java项目几乎相同 。<br/>
            To build a Groovy project, you use the <em class="firstterm">Groovy plugin</em>
            . This plugin extends the Java
        plugin to add Groovy compilation capabilities to your project. Your project can contain Groovy source code,
        Java source code, or a mix of the two. In every other respect, a Groovy project is identical to a Java project,
        which we have already seen in
            <a class="xref" href="../ch07/tutorial_java_projects.html">
                Chapter&nbsp;7, <i>Java Quickstart</i>
            </a>
            .
        </p>
        <div class="section">
            <div class="titlepage">
                <div>
                    <div>
                        <h2 class="title">
                            <a name="N106BE"></a>
                            9.1. 一个基本的Groovy 项目
                        </h2>
                        <h4>9.1.&nbsp;A basic Groovy project</h4>
                    </div>
                </div>
            </div>
            <p>
                让我们来看一个例子。要使用Groovy插件，你需要在构建脚本文件当中添加以下内容：<br/>
                Let's look at an example. To use the Groovy plugin, add the following to your build file:
            </p>
            <div class="example">
                <a name="groovyQuickstart"></a>
                <p class="title"> <b>Example&nbsp;9.1.&nbsp;Groovy plugin</b>
                </p>
                <div class="example-contents">
                    <p>
                        <code class="filename">build.gradle</code>
                    </p>
                    <pre class="programlisting">apply plugin: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'groovy'</span></pre>
                    <div class="exampleLocation">
                        <p>
                            <span class="emphasis"> <em>注意：</em></span> 此例子的代码可以在Gradle的二进制文件或源码中的 <code class="filename">samples/groovy/quickstart</code> 里看到。<br/>
                            <span class="emphasis"> <em>Note:</em>
                            </span>
                            The code for this example can be found at
                            <code class="filename">samples/groovy/quickstart</code>
                            which is in both the binary and source distributions of Gradle.
                        </p>
                    </div>
                </div>
            </div>
            <br class="example-break">
            <p>
                这段代码同时会将Java插件应用到project中，如果Java插件还没被应用的话。Groovy插件继承了 <code class="literal">compile</code> 任务 ，在 <code class="filename">src/main/groovy</code> 目录中查找源文件；且继承了 <code class="literal">compileTest</code> 任务，在 <code class="filename">src/test/groovy</code> 目录中查找测试的源文件。这些编译任务对这些目录使用了联合编译，这意味着它们可以同时包含java和groovy源文件。<br/>
                This will also apply the Java plugin to the project, if it has not already been applied. The Groovy plugin
            extends the
                <code class="literal">compile</code>
                task to look for source files in directory
                <code class="filename">src/main/groovy</code>
                , and the
                <code class="literal">compileTest</code>
                task to look for test source
            files in directory
                <code class="filename">src/test/groovy</code>
                . The compile tasks use joint compilation for these
            directories, which means they can contain a mixture of java and groovy source files.
            </p>
            <p>
                要使用groovy编译任务，还必须声明要使用的Groovy版本以及从哪里获取Groovy库。你可以通过在 <code class="literal">groovy</code> 配置中添加依赖来完成。<code class="literal">compile</code> 配置继承了这个依赖,从而在编译Groovy和Java源代码时，groovy库也会被包含在类路径中。下面例子中，我们会使用Maven中央仓库中的Groovy 2.2.0版本。<br/>
                To use the groovy compilation tasks, you must also declare the Groovy version to use and where to find the
            Groovy libraries. You do this by adding a dependency to the
                <code class="literal">groovy</code>
                configuration.
            The
                <code class="literal">compile</code>
                configuration inherits this dependency, so the groovy libraries will
            be included in classpath when compiling Groovy and Java source.  For our sample, we will use Groovy 2.2.0
            from the public Maven repository:
            </p>
            <div class="example">
                <a name="groovyQuickstart"></a>
                <p class="title"> <b>Example&nbsp;9.2.&nbsp;Dependency on Groovy 2.2.0</b>
                </p>
                <div class="example-contents">
                    <p>
                        <code class="filename">build.gradle</code>
                    </p>
                    <pre class="programlisting">repositories {
    mavenCentral()
}

dependencies {
    compile <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'org.codehaus.groovy:groovy-all:2.2.0'</span>
}</pre>
                </div>
            </div>
            <br class="example-break">
            <p>
                这里是我们写好的构建文件：<br/>
                Here is our complete build file:</p>
            <div class="example">
                <a name="groovyQuickstart"></a>
                <p class="title">
                    <b>Example&nbsp;9.3.&nbsp;Groovy example - complete build file</b>
                </p>
                <div class="example-contents">
                    <p>
                        <code class="filename">build.gradle</code>
                    </p>
                    <pre class="programlisting">apply plugin: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'eclipse'</span>
apply plugin: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'groovy'</span>

repositories {
    mavenCentral()
}

dependencies {
    compile <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'org.codehaus.groovy:groovy-all:2.2.0'</span>
    testCompile <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'junit:junit:4.11'</span>
}</pre>
                </div>
            </div>
            <br class="example-break">
            <p>
                运行 <strong class="userinput"><code>gradle build</code></strong> 将会对你的项目进行编译，测试和打成jar包。<br/>
                Running <strong class="userinput"><code>gradle build</code></strong> 
                will compile, test and JAR your project.
            </p>
        </div>
        <div class="section">
            <div class="titlepage">
                <div>
                    <div>
                        <h2 class="title">
                            <a name="N10707"></a>
                            9.2. 总结
                        </h2>
                        <h4>9.2.&nbsp;Summary</h4>
                    </div>
                </div>
            </div>
            <p>
                这一章描述了一个很简单的Groovy项目。通常情况下，一个真实的项目所需要的不止于此。因为一个Groovy项目也 <span class="emphasis"><em>是 </em></span>一个Java项目，因此你能用Java做的事情Groovy也能做。<br/>
                This chapter describes a very simple Groovy project. Usually, a real project will require more than this.
            Because a Groovy project
                <span class="emphasis">
                    <em>is</em>
                </span>
                a Java project, whatever you can do with a Java project,
            you can also do with a Groovy project.
            </p>
            <p>
                你可以参阅 <a class="xref" href="../ch24/groovy_plugin.html">第24章 <i>Groovy插件</i></a> 去了解更多关于Groovy 插件的内容，或在Gradle发行包的 <code class="filename">samples/groovy</code> 目录中找到更多的Groovy 项目示例。<br/>
                You can find out more about the Groovy plugin in
                <a class="xref" href="../ch24/groovy_plugin.html">
                    Chapter&nbsp;24, <i>The Groovy Plugin</i>
                </a>
                , and you can find more
            sample Groovy projects in the
                <code class="filename">samples/groovy</code>
                directory in the Gradle distribution.
            </p>
        </div>
    </div>
    <div class="navfooter">
        <div>
            <div class="navbar">
                <a xmlns:xslthl="http://xslthl.sf.net" href="../ch08/artifact_dependencies_tutorial.html" title="第八章. 依赖管理基础">上一章</a>
                <span>|</span>
                <a xmlns:xslthl="http://xslthl.sf.net" href="../userguide.html" title="用户手册">目录</a>
                <span>|</span>
                <a xmlns:xslthl="http://xslthl.sf.net" href="../ch10/web_project_tutorial.html" title="第十章. Web 程序快速入门">下一章</a>
            </div>
        </div>
    </div>
</body>
</html>